﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace LibraryUtility.Web
{
    /// <summary>
    /// ClassName:ConfigHelper
    /// Auther:Leo.wl
    /// Time:2010-04-14
    /// web.config操作类
    /// </summary>
    public sealed  class ConfigHelper
    {

        /// <summary>
        /// 得到AppSettings中的配置字符串信息.
        /// </summary>
        /// <param name="Key">The key.</param>
        /// <returns></returns>
        public static string GetConfigString(string Key)
        {
            return ConfigurationSettings.AppSettings[Key];             
        }

        /// <summary>
        /// 得到AppSettings中的配置bool信息.
        /// Gets the config bool.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static bool GetConfigBool(string key)
        {
            bool result = false;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = bool.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }

            return result;
        }

        /// <summary>
        /// 得到AppSettings中的配置decimal信息.
        /// Gets the config decimal.
        /// </summary>
        /// <param name="Key">The key.</param>
        /// <returns></returns>
        public static decimal GetConfigDecimal(string Key)
        {
            decimal result = 0;
            string cfgVal = GetConfigString(Key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = decimal.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }

            return result;
        }

        /// <summary>
        /// 得到AppSettings中的配置int信息.
        /// Gets the config int.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static int GetConfigInt(string key)
        {
            int result = 0;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }

            return result;
        }
    }
}
